package com.yycx.dbr.boot.admin.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.client.entity.BaseUserCompany;
import com.yycx.module.admin.provider.service.BaseRoleService;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.client.entity.SysOrganization;
import com.yycx.module.system.provider.service.SysCompanyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台用户列表扩展(加入机构ID)
 *
 * @author zyf
 */
@Component
@AllArgsConstructor
public class HyAdminUserPageListHandler implements PageInterceptor<BaseUser> {

    private final SysCompanyService companyService;
    private final BaseRoleService roleService;
    private final BaseUserService userService;

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Long organizationId = cq.getLong("organizationId");
        cq.clear();
        //cq.select(SysCompany.class, "companyName");
        cq.select("DISTINCT baseuser.userId","baseuser.userName","baseuser.nickName","baseuser.hostName","baseuser.userNo","baseuser.companyId","baseuser.customDeptName","baseuser.applyName","baseuser.tenantId","baseuser.organizationId","baseuser.createTime");
        cq.eq(SysCompany.class, "companyId");
        Long companyId = OpenHelper.getCompanyId();
        Integer superAdmin = OpenHelper.getUser().getSuperAdmin();
        if (CommonConstants.INT_1.equals(superAdmin)) {
            //如果是超级管理员
            cq.eq(BaseUser.class, "organizationId");
        } else {
            SysCompany company = companyService.getById(companyId);
            if (FlymeUtils.isNotEmpty(company) && FlymeUtils.isEmpty(organizationId)) {
                cq.eq(BaseUserCompany.class, "companyId", company.getCompanyId());
            } else {
                cq.eq(BaseUser.class, "organizationId", company.getOrganizationId());
            }
            //向下传递租户ID
            params.put("tenantId", company.getTenantId());
        }
        cq.createJoin(SysCompany.class);
        cq.createJoin(BaseUserCompany.class).setJoinField("userId").setMainField("userId");

    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BaseUser> cq, List<EntityMap> result, EntityMap extra) {
        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                String tenantId = entityMap.get("tenantId");
                Long userId = entityMap.getLong("userId");
                List<String> roleNames = roleService.selectRoleNamesByUserId(userId, tenantId);
                if (FlymeUtils.isNotEmpty(roleNames)) {
                    entityMap.put("roleNames", ConvertUtils.listToString(roleNames));
                }
            }
        }
        return result;
    }
}
